<?php

namespace App\Http\Controllers\Admin;

use App\Model\Banner;
use App\Observers\PictureObserver;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class PictureController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct(PictureObserver $pictureObserver)
    {
        $this->pictureObserver = $pictureObserver;
    }

    public function index()
    {
        $item = Banner::orderBy('id','asc')->get();
        $total = Banner::count();
        return view('admin.picture.picture',['item'=>$item,'total'=>$total]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.picture.picture-add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(),[
            'title' => 'required'
        ],[
            'title.required' => '标题不能为空'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }

        $this->pictureObserver->handleUploadedImage($request->file('file'));

        if($this->pictureObserver->handleUploadedImage($request->file('file'))) {
            $m = new Banner();
            $m->title = $request->input('title');
            $m->link = $request->input('link');
            $m->image = $this->pictureObserver->path;
            $m->des = $request->input('des');
            $m->order = $request->input('order');
            if($m->save()) {
                return $this->success('添加成功');
            }

            return $this->error('添加失败');
        }

        return $this->error($this->pictureObserver->getFirstError());

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $item = Banner::where('id',$id)->first();
        return view('admin.picture.picture-edit',['item'=>$item]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(),[
            'title' => 'required'
        ],[
            'title.required' => '标题不能为空'
        ]);
        if ($validator->fails()){
            return $this->error($validator->errors()->first());
        }
        if ($request->hasFile('file')){
            $file = $request->file('file');
            $validArr = ['png','jpg','jpeg','gif'];
            if (!in_array($file->getClientOriginalExtension(),$validArr)){
                return $this->error('不支持的图片格式');
            }
            $path = $request->file->store('uploads'.'/'.date('Y-m-d'));
        }else{
            $img = $request->input('img');
        }
        $m = Banner::find($id);
        $m->title = $request->input('title');
        $m->link = $request->input('link');
        $m->image = !empty($path) ? $path : $img;
        $m->des = $request->input('des');
        $m->order = $request->input('order');
        if ($m->save()){
            return $this->success('更改成功');
        }else{
            return $this->error('更改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = Banner::find($id)->delete();
        if ($data){
            return $this->success('删除成功');
        }else{
            return $this->error('删除失败');
        }
    }
}
